package chloroform.util

import chloroform.PrettyPrintRenderer
import java.io.StringWriter
import argo.format.PrettyJsonFormatter
import argo.jdom.{JsonNodeBuilder, JsonRootNode}

/**
 * User: zslajchrt
 * Date: 24.2.11
 * Time: 17:06
 */

object JsonRendererUtil {
  def makeRenderer(msgJsonBuilder: JsonNodeBuilder[JsonRootNode]) = {
    new PrettyPrintRenderer() {
      def builder(): JsonNodeBuilder[JsonRootNode] = msgJsonBuilder
    }
  }

  def renderToString(msgJsonBuilder: JsonNodeBuilder[JsonRootNode]): String = {
    val renderer = makeRenderer(msgJsonBuilder)
    val writer = new StringWriter()
    renderer.render(writer)
    writer.close
    writer.toString
  }

  def renderNodeToString(node: JsonRootNode): String = {
    val formatter = new PrettyJsonFormatter
    val swr = new StringWriter
    formatter.format(node, swr)
    swr.toString
  }

}